<?php

/**
 * This is the model class for table "{{contact}}".
 *
 * The followings are the available columns in table '{{contact}}':
 * @property integer $id
 * @property string $name
 * @property string $email
 * @property string $phone
 * @property string $title
 * @property string $content
 * @property integer $status
 * @property integer $language_id
 * @property integer $type
 * @property integer $created_at
 */
class Contact extends MyARContent
{

    public $verifyCode;

    const STATUS_NEW       = 0;
    const STATUS_CONFIRM   = 1;
    const STATUS_COMPLETED = 2;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Contact the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return '{{contact}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name, content, email, title', 'required'),
            array('verifyCode', 'required', 'on' => 'useCaptcha'),
            array('status, language_id, type, created_at', 'numerical', 'integerOnly' => true),
            array('email', 'email'),
            array('name, email, phone, title', 'length', 'max' => 200),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, name, email, phone, title, content, status, language_id, type, created_at', 'safe', 'on' => 'search'),
            array('verifyCode', 'captcha', 'allowEmpty' => !CCaptcha::checkRequirements(), 'on' => 'useCaptcha'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.

        $relations = parent::relations();
        return $relations;
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        $array = array(
            'id'          => 'ID',
            'name'        => 'Tên người gửi',
            'email'       => 'Email',
            'phone'       => 'Điện thoại',
            'title'       => 'Tiêu đề',
            'content'     => 'Nội dung',
            'status'      => 'Trạng thái',
            'language_id' => 'Language',
            'type'        => 'Type',
            'verifyCode'  => 'Verify Code',
            'created_at'  => 'Created At',
        );
        foreach ($array as $key => $val)
        {
            $array[$key] = Yii::t('sub_all', $val);
        }
        return $array;
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('email', $this->email, true);
        $criteria->compare('phone', $this->phone, true);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('content', $this->content, true);
        $criteria->compare('status', $this->status);
        $criteria->compare('language_id', $this->language_id);
        $criteria->compare('type', $this->type);
        $criteria->compare('created_at', $this->created_at);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public static function getContact()
    {
        $cri            = new CDbCriteria();
        $cri->condition = 'type=:ty';
        $cri->params    = array(
            ':ty' => self::STATUS_NEW,
        );
        return self::model()->count($cri);
    }

    public static function getArrayStatus()
    {
        return array(
            self::STATUS_NEW       => yii::t('myarcontent', 'Liên hệ mới'),
            self::STATUS_CONFIRM   => yii::t('myarcontent', 'Liên hệ đã xem'),
            self::STATUS_COMPLETED => yii::t('myarcontent', 'Liên hệ đã xem'),
        );
    }

    public function getStatusLabel()
    {
        if (!$this->hasAttribute("status"))
            return '';

        $labelClass = 'label ';
        switch ($this->status)
        {
            case self::STATUS_NEW:
                $labelClass .= 'label-important';
                break;
            case self::STATUS_CONFIRM:
                $labelClass .= 'label-success';
                break;
            case self::STATUS_COMPLETED:
                $labelClass .= 'label-info';
                break;

            default:
                break;
        }
        $labels = self::getArrayStatus();
        $label  = isset($labels[$this->status]) ? $labels[$this->status] : $this->status;
        return "<span class='$labelClass'>$label</span>";
    }

    public function getFormatDate()
    {
        return date('d/m/Y', $this->created_at);
    }

    public static function getCountNew()
    {
        $criteria            = new CDbCriteria;
        $criteria->condition = "status =:s";
        $criteria->params    = array(
            ':s' => Contact::STATUS_NEW
        );

        return self::model()->count($criteria);
    }

}
